import api from './api'
import isLogin from '../utils/isLogin'
const indexApi = {
	// 模拟排行榜 实际还是请求的全部列表
	showRank: () => {
		return api
			.get('/index/article')
			.then(res => {
				return res.data.data.splice(0, 10)
			})
			.catch(err => {
				console.log(err)
				return []
			})
	},
	// 获取对应id的文章内容
	getNewsDetails: id => {
		return api.get(`/index/article/${id}`).then(res => {
			return res.data.data
		})
	},
	// 设置文章点赞
	setNewsSupport: id => {
		return api.put(`/users/articleSupport/${id}`).then(res => {
			return res.data
		})
	},
	// 获取是否点赞该文章
	getNewsSupport: id => {
		return api.get(`/users/articleSupport/${id}`).then(res => {
			return res.data.type
		})
	},
	// 点赞文章评论
	setNewsCommentSupport: id => {
		return api.put(`/users/articleCommentSupport/${id}`).then(res => {
			return res.data
		})
	},
	// 获取评论内容
	getNewsComment: id => {
		if (isLogin()) {
			return api.get(`/users/article/comment/${id}`).then(res => {
				return res.data.data || []
			})
		} else {
			return api.get(`/index/article/comment/${id}`).then(res => {
				return res.data.data || []
			})
		}
	},
	// 添加文章评论
	setNewsComment: (article_id, article_title, context) => {
		return api
			.post(`/users/articleComment`, {
				article_id,
				article_title,
				context
			})
			.then(res => {
				if (res.data.status === 0) {
					AMessage.success(res.data.message)
				} else {
					res.data.errors.forEach(x => {
						AMessage.error(x.msg)
					})
				}
			})
	}
}
export default indexApi
